import requests
import os
import json
import time
import argparse
from enum import Enum
from urllib.parse import quote
import urllib3
from collections import namedtuple

from utils.configs import CodeartsConfig, GiteeConfig, NEED_REPORT_CI_JOBS
from check_runs2 import create_check, update_check
from web_request_process.gitee_process import GiteePrLabelProcess, GiteePrCommentProcess, GiteePrInfoProcess

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


def add_ci_check_to_pr(merge_id):
    pr_info = GiteePrInfoProcess(merge_id)
    pr_info = pr_info.get_info()
    pr_head_sha = GiteePrInfoProcess.get_head_sha(pr_info)
    pr_id = GiteePrInfoProcess.get_pull_request_id(pr_info)
    print("开始添加门禁检查项，状态为排队中...")
    for _, ci_jobs in NEED_REPORT_CI_JOBS.items():
        for _, ci_job_info in ci_jobs.items():
            time.sleep(2)
            create_check(GiteeConfig.REPO_OWNER, GiteeConfig.REPO_NAME, merge_id, ci_job_info.ci_check_item_name,
                         head_sha=pr_head_sha, pull_request_id=pr_id)
            time.sleep(3)
            update_check(GiteeConfig.REPO_OWNER, GiteeConfig.REPO_NAME,
                         merge_id, ci_job_info.ci_check_item_name, "queued",
                         pull_request_id=pr_id)


def pre_ci_process(merge_id):
    add_ci_check_to_pr(merge_id)


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument("--pipeline_run_id", type=str,
                        default="b56c4975eeef414e86bdb8e988920704")
    parser.add_argument("--merge_id", type=str,
                        help="MR Id or PR id", default="1")
    args, _ = parser.parse_known_args()

    # 读取所有环境变量
    env_vars = os.environ
    # 打印每个环境变量的名称和值
    print("打印环境变量...")
    for var, value in env_vars.items():
        print(f'{var}: {value}')

    print("开始添加门禁检查项...")
    pre_ci_process(args.merge_id)
